home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / indentsr / indent_g.h < prev    next >
C/C++ Source or Header  |  1990-01-04  |  12KB  |  287 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that the above copyright notice and this paragraph are
  9.  * duplicated in all such forms and that any documentation,
  10.  * advertising materials, and other materials related to such
  11.  * distribution and use acknowledge that the software was developed
  12.  * by the University of California, Berkeley, the University of Illinois,
  13.  * Urbana, and Sun Microsystems, Inc.  The name of either University
  14.  * or Sun Microsystems may not be used to endorse or promote products
  15.  * derived from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  *
  20.  *    @(#)indent_globs.h    5.7 (Berkeley) 9/15/88
  21.  */
  22.  
  23. #include <stdio.h>
  24.  
  25. #define BACKSLASH '\\'
  26. #define bufsize 200        /* size of internal buffers */
  27. #define inp_bufs 600        /* size of input buffer */
  28. #define sc_size 5000        /* size of save_com buffer */
  29. #define label_offset 2        /* number of levels a label is placed to left
  30.                  * of code */
  31.  
  32. #define tabsize 8        /* the size of a tab */
  33. #define tabmask 0177770        /* mask used when figuring length of lines
  34.                  * with tabs */
  35.  
  36.  
  37. #define false 0
  38. #define true  1
  39.  
  40.  
  41. FILE       *input;        /* the fid for the input file */
  42. FILE       *output;        /* the output file */
  43.  
  44. #ifdef MSDOS
  45. /* RjR: changed to ANSI token-pasting */
  46. #define check_size(name) \
  47.     if (e_##name >= l_##name) { \
  48.         register nsize = l_##name-s_##name+400; \
  49.         name##buf = (char *) realloc(name##buf, nsize); \
  50.         e_##name = name##buf + (e_##name-s_##name) + 1; \
  51.         l_##name = name##buf + nsize - 5; \
  52.         s_##name = name##buf + 1; \
  53.     }
  54. #else
  55. #define check_size(name) \
  56.     if (e_/**/name >= l_/**/name) { \
  57.         register nsize = l_/**/name-s_/**/name+400; \
  58.         name/**/buf = (char *) realloc(name/**/buf, nsize); \
  59.         e_/**/name = name/**/buf + (e_/**/name-s_/**/name) + 1; \
  60.         l_/**/name = name/**/buf + nsize - 5; \
  61.         s_/**/name = name/**/buf + 1; \
  62.     }
  63. #endif
  64.  
  65. /* RjR: added for Mess-DOS */
  66. #ifdef MSDOS
  67. #define bcopy(a,b,c) memmove(b,a,c)
  68. #define bzero(a,b) memset(a,'\0',b)
  69. #endif
  70.  
  71. char       *labbuf;        /* buffer for label */
  72. char       *s_lab;        /* start ... */
  73. char       *e_lab;        /* .. and end of stored label */
  74. char       *l_lab;        /* limit of label buffer */
  75.  
  76. char       *codebuf;        /* buffer for code section */
  77. char       *s_code;        /* start ... */
  78. char       *e_code;        /* .. and end of stored code */
  79. char       *l_code;        /* limit of code section */
  80.  
  81. char       *combuf;        /* buffer for comments */
  82. char       *s_com;        /* start ... */
  83. char       *e_com;        /* ... and end of stored comments */
  84. char       *l_com;        /* limit of comment buffer */
  85.  
  86. char        in_buffer[inp_bufs];/* input buffer */
  87. char       *buf_ptr;        /* ptr to next character to be taken from
  88.                  * in_buffer */
  89. char       *buf_end;        /* ptr to first after last char in in_buffer */
  90.  
  91. char        save_com[sc_size];    /* input text is saved here when looking for
  92.                  * the brace after an if, while, etc */
  93. char       *sc_end;        /* pointer into save_com buffer */
  94.  
  95. char       *bp_save;        /* saved value of buf_ptr when taking input
  96.                  * from save_com */
  97. char       *be_save;        /* similarly saved value of buf_end */
  98.  
  99. char        token[bufsize];    /* the last token scanned */
  100.  
  101.  
  102. int         pointer_as_binop;
  103. int         blanklines_after_declarations;
  104. int         blanklines_before_blockcomments;
  105. int         blanklines_after_procs;
  106. int         blanklines_around_conditional_compilation;
  107. int         swallow_optional_blanklines;
  108. int         n_real_blanklines;
  109. int         prefix_blankline_requested;
  110. int         postfix_blankline_requested;
  111. int         break_comma;    /* when true and not in parens, break after a
  112.                  * comma */
  113. int         btype_2;        /* when true, brace should be on same line as
  114.                  * if, while, etc */
  115. float       case_ind;        /* indentation level to be used for a "case
  116.                  * n:" */
  117. int         code_lines;        /* count of lines with code */
  118. int         had_eof;        /* set to true when input is exhausted */
  119. int         line_no;        /* the current line number. */
  120. int         max_col;        /* the maximum allowable line length */
  121. int         verbose;        /* when true, non-essential error messages are
  122.                  * printed */
  123. int         cuddle_else;    /* true if else should cuddle up to '}' */
  124. int         star_comment_cont;    /* true iff comment continuation lines should
  125.                  * have stars at the beginning of each line. */
  126. int         comment_delimiter_on_blankline;
  127. int         troff;        /* true iff were generating troff input */
  128. int         procnames_start_line;    /* if true, the names of procedures
  129.                      * being defined get placed in column
  130.                      * 1 (ie. a newline is placed between
  131.                      * the type of the procedure and its
  132.                      * name) */
  133. int         proc_calls_space;    /* If true, procedure calls look like:
  134.                  * foo(bar) rather than foo (bar) */
  135. int         format_col1_comments;    /* If comments which start in column 1
  136.                      * are to be magically reformatted
  137.                      * (just like comments that begin in
  138.                      * later columns) */
  139. int         inhibit_formatting;    /* true if INDENT OFF is in effect */
  140. int         suppress_blanklines;/* set iff following blanklines should be
  141.                  * suppressed */
  142. int         continuation_indent;/* set to the indentation between the edge of
  143.                  * code and continuation lines */
  144. int         lineup_to_parens;    /* if true, continued code within parens will
  145.                  * be lined up to the open paren */
  146. int         Bill_Shannon;    /* true iff a blank should always be inserted
  147.                  * after sizeof */
  148. int         blanklines_after_declarations_at_proctop;    /* This is vaguely
  149.                              * similar to
  150.                              * blanklines_after_decla
  151.                              * rations except that
  152.                              * it only applies to
  153.                              * the first set of
  154.                              * declarations in a
  155.                              * procedure (just after
  156.                              * the first '{') and it
  157.                              * causes a blank line
  158.                              * to be generated even
  159.                              * if there are no
  160.                              * declarations */
  161. int         block_comment_max_col;
  162. int         extra_expression_indent;    /* True if continuation lines from the
  163.                      * expression part of "if(e)",
  164.                      * "while(e)", "for(e;e;e)" should be
  165.                      * indented an extra tab stop so that
  166.                      * they don't conflict with the code
  167.                      * that follows */
  168.  
  169. /* -troff font state information */
  170.  
  171. struct fstate {
  172.     char        font[4];
  173.     char        size;
  174.     int         allcaps:1;
  175. };
  176. char       *chfont();
  177.  
  178. struct fstate
  179.             keywordf,        /* keyword font */
  180.             stringf,        /* string font */
  181.             boxcomf,        /* Box comment font */
  182.             blkcomf,        /* Block comment font */
  183.             scomf,        /* Same line comment font */
  184.             bodyf;        /* major body font */
  185.  
  186.  
  187. #define STACKSIZE 150
  188.  
  189. struct parser_state {
  190.     int         last_token;
  191.     struct fstate cfont;    /* Current font */
  192.     int         p_stack[STACKSIZE];    /* this is the parsers stack */
  193.     int         il[STACKSIZE];    /* this stack stores indentation levels */
  194.     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
  195.     int         box_com;    /* set to true when we are in a "boxed"
  196.                  * comment. In that case, the first non-blank
  197.                  * char should be lined up with the / in /* */
  198.     int         comment_delta,
  199.                 n_comment_delta;
  200.     int         cast_mask;    /* indicates which close parens close off
  201.                  * casts */
  202.     int         sizeof_mask;    /* indicates which close parens close off
  203.                  * sizeof''s */
  204.     int         block_init;    /* true iff inside a block initialization */
  205.     int         block_init_level;    /* The level of brace nesting in an
  206.                      * initialization */
  207.     int         last_nl;    /* this is true if the last thing scanned was
  208.                  * a newline */
  209.     int         in_or_st;    /* Will be true iff there has been a
  210.                  * declarator (e.g. int or char) and no left
  211.                  * paren since the last semicolon. When true,
  212.                  * a '{' is starting a structure definition or
  213.                  * an initialization list */
  214.     int         bl_line;    /* set to 1 by dump_line if the line is blank */
  215.     int         col_1;        /* set to true if the last token started in
  216.                  * column 1 */
  217.     int         com_col;    /* this is the column in which the current
  218.                  * coment should start */
  219.     int         com_ind;    /* the column in which comments to the right
  220.                  * of code should start */
  221.     int         com_lines;    /* the number of lines with comments, set by
  222.                  * dump_line */
  223.     int         dec_nest;    /* current nesting level for structure or init */
  224.     int         decl_com_ind;    /* the column in which comments after
  225.                  * declarations should be put */
  226.     int         decl_on_line;    /* set to true if this line of code has part
  227.                  * of a declaration on it */
  228.     int         i_l_follow;    /* the level to which ind_level should be set
  229.                  * after the current line is printed */
  230.     int         in_decl;    /* set to true when we are in a declaration
  231.                  * stmt.  The processing of braces is then
  232.                  * slightly different */
  233.     int         in_stmt;    /* set to 1 while in a stmt */
  234.     int         ind_level;    /* the current indentation level */
  235.     int         ind_size;    /* the size of one indentation level */
  236.     int         ind_stmt;    /* set to 1 if next line should have an extra
  237.                  * indentation level because we are in the
  238.                  * middle of a stmt */
  239.     int         last_u_d;    /* set to true after scanning a token which
  240.                  * forces a following operator to be unary */
  241.     int         leave_comma;    /* if true, never break declarations after
  242.                  * commas */
  243.     int         ljust_decl;    /* true if declarations should be left
  244.                  * justified */
  245.     int         out_coms;    /* the number of comments processed, set by
  246.                  * pr_comment */
  247.     int         out_lines;    /* the number of lines written, set by
  248.                  * dump_line */
  249.     int         p_l_follow;    /* used to remember how to indent following
  250.                  * statement */
  251.     int         paren_level;    /* parenthesization level. used to indent
  252.                  * within stmts */
  253.     short       paren_indents[20];    /* column positions of each paren */
  254.     int         pcase;        /* set to 1 if the current line label is a
  255.                  * case.  It is printed differently from a
  256.                  * regular label */
  257.     int         search_brace;    /* set to true by parse when it is necessary
  258.                  * to buffer up all info up to the start of a
  259.                  * stmt after an if, while, etc */
  260.     int         unindent_displace;    /* comments not to the right of code
  261.                      * will be placed this many
  262.                      * indentation levels to the left of
  263.                      * code */
  264.     int         use_ff;        /* set to one if the current line should be
  265.                  * terminated with a form feed */
  266.     int         want_blank;    /* set to true when the following token should
  267.                  * be prefixed by a blank. (Said prefixing is
  268.                  * ignored in some cases.) */
  269.     int         else_if;    /* True iff else if pairs should be handled
  270.                  * specially */
  271.     int         decl_indent;    /* column to indent declared identifiers to */
  272.     int         its_a_keyword;
  273.     int         sizeof_keyword;
  274.     int         dumped_decl_indent;
  275.     float       case_indent;    /* The distance to indent case labels from the
  276.                  * switch statement */
  277.     int         in_parameter_declaration;
  278.     int         indent_parameters;
  279.     int         tos;        /* pointer to top of stack */
  280.     char        procname[100];    /* The name of the current procedure */
  281.     int         just_saw_decl;
  282. }           ps;
  283.  
  284. int         ifdef_level;
  285. struct parser_state state_stack[5];
  286. struct parser_state match_state[5];
  287.